//------------------------------------------------------------------------
// Abstract: Implement of class XWrapper
//
// Authoring: XiaoHanxiong
//
// Time: 2014.4.09
//
// Static test:
//
// Version history:
//		Ver_1.0 : 2014.4.09	XiaoHanxiong
//------------------------------------------------------------------------

#include "Stdafx.h"
#include "XWrapper.h"

namespace Library
{
	XWrapper::XWrapper()
	{
		m_cursor = m_memory.Data();
	}

	XWrapper::~XWrapper()
	{

	}

	/*
		Input data
	*/
	XWrapper& XWrapper::operator << ( UINT8 value )
	{
		m_memory.PushBack(value);
		m_cursor += 1;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( UINT16 value )
	{
		m_memory.PushBack(value);
		m_cursor += 2;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( UINT32 value )
	{
		m_memory.PushBack(value);
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( UINT64 value )
	{
		m_memory.PushBack((UINT8*)&value, 8);
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( INT8 value )
	{
		m_memory.PushBack((UINT8)value);
		m_cursor += 1;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( INT16 value )
	{
		m_memory.PushBack((UINT16)value);
		m_cursor += 2;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( INT32 value )
	{
		m_memory.PushBack((UINT32)value);
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( INT64 value )
	{
		m_memory.PushBack((UINT8*)&value, 8);
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( float value )
	{
		m_memory.PushBack(value);
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( double value )
	{
		m_memory.PushBack(value);
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator << ( string& str )
	{
		m_memory.PushBack((UINT16)str.size());
		m_memory.PushBack((UINT8*)str.c_str(), str.size());
		m_cursor += 2 + str.size();
		return(*this);
	}

	/*
		Output data
	*/

	XWrapper& XWrapper::operator >> ( UINT8& value )
	{
		value = *m_cursor;
		m_cursor += 1;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( UINT16& value )
	{
		value = *(const UINT16*)m_cursor;
		m_cursor += 2;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( UINT32& value )
	{
		value = *(const UINT32*)m_cursor;
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( UINT64& value )
	{
		value = *(const UINT64*)m_cursor;
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( INT8& value )
	{
		value = *(const INT8*)m_cursor;
		m_cursor += 1;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( INT16& value )
	{
		value = *(const INT16*)m_cursor;
		m_cursor += 2;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( INT32& value )
	{
		value = *(const INT32*)m_cursor;
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( INT64& value )
	{
		value = *(const INT64*)m_cursor;
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( float& value )
	{
		value = *(const float*)m_cursor;
		m_cursor += 4;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( double& value )
	{
		value = *(const double*)m_cursor;
		m_cursor += 8;
		return(*this);
	}

	XWrapper& XWrapper::operator >> ( string& str )
	{
		size_t size = *(const UINT16*)m_cursor;
		m_cursor += 2;
		char* buffer = new char[size + 1]();
		memcpy(buffer, m_cursor, size);
		str = buffer;
		m_cursor += size;
		delete [] buffer;
		return(*this);
	}

	size_t XWrapper::GetLength()
	{
		return (m_memory.Size());
	}

	const void* XWrapper::GetData()
	{
		return(m_memory.Data());
	}

	void XWrapper::SeekBegin()
	{
		m_cursor = m_memory.Begin();
	}

	void XWrapper::SeekEnd()
	{
		m_cursor = m_memory.End();
	}

	void XWrapper::PushBlob( void* blob, size_t size )
	{
		if(blob == NULL) return;
		else m_memory.PushBack((UINT8*)blob, size);
	}

	void XWrapper::Clear()
	{
		m_memory.Clear();
	}

	void XWrapper::IncreaseFront( size_t front_size )
	{
		m_memory.Increase(front_size, static_cast<size_t>(m_memory.End() - m_memory.Zero()));
	}

	void XWrapper::IncreaseBack( size_t back_size )
	{
		m_memory.Increase(static_cast<size_t>(m_memory.Zero() - m_memory.Begin()), back_size);
	}
}